#include "ground.h"
#include <cstdlib>
#include <cmath>

Ground::Ground(int width, int height)
{
    world_height = height;
    world_width = width;
    heightmap.reserve(width);
}

Ground::Ground()
{
    world_width = 1024;
    world_height = 768;
    heightmap.reserve(world_width);
}

int Ground::getHeight(int x)
{
    return heightmap[x];
}

void Ground::setHeight(int x,int height)
{
    heightmap[x] = height;
}

void Ground::setHeightDelta(int x,int delta)
{
    heightmap[x] += delta;
}

void Ground::generateMap()
{
    heightmap[0] = 150;
    for (int i=1;i < world_width;++i)
	{
		heightmap[i] = (rand()%11-5)+heightmap[i-1];
	}
	for (int i = 1; i < world_width-1;i += 2)
	{
	    // This value is halfway between the other two.
	    heightmap[i] = floor((heightmap[i+1]-heightmap[i-1])/2)+heightmap[i-1];
	}
}
